# ------------------------------------ SNAKE ------------------------------------

INCLUDE		=	-I. -I./include
SOURCE		=	src

#   compilers
CC			=	arm-elf-gcc
LOADER		=	at200

#   flags
WARNINGS	=	-Wall -Wstrict-prototypes -Wno-trigraphs
CFLAGS		=	$(INCLUDE) $(WARNINGS) -O2 -mcpu=arm7tdmi 
LDFLAGS		=	-nostartfiles -N -Tlpc2292.ld -Xlinker -Map=$(OUTPUT).map
LOADERFLAGS	=	$(COMPORT) -baud $(BAUD) -osc $(OSC) -LPC2292

#   com settings
COMPORT = com1
BAUD    = 38400
OSC		= 14745

#  
#   compiled files 
SRC_FILES = \
$(SOURCE)/KB.c \
$(SOURCE)/lcd.c \
$(SOURCE)/led.c \
$(SOURCE)/main.c \
$(SOURCE)/init.c \
$(SOURCE)/snake.c \
$(SOURCE)/sound.c \
$(SOURCE)/serial.c \
$(SOURCE)/lcdtext.c \
$(SOURCE)/drawable.c \
$(SOURCE)/systimer.c \
$(SOURCE)/interrupt.c \
$(SOURCE)/snakeFood.c \
$(SOURCE)/snakeSound.c \
$(SOURCE)/snakeLogic.c \
$(SOURCE)/back_buffer.c \
$(SOURCE)/snakeRender.c \

OUTPUT		=	Test_SDK_2_0

OBJ_FILES	=	$(SRC_FILES:.c=.o)

#--------------------------------------------------------------------------------
#	make and load
#--------------------------------------------------------------------------------
all: load

#--------------------------------------------------------------------------------
#	compiling
#--------------------------------------------------------------------------------
$(OUTPUT): $(SOURCE)/startup.o $(OBJ_FILES)
		$(CC) $(SOURCE)/startup.o $(OBJ_FILES) $(LDFLAGS) -o $(OUTPUT)
		arm-elf-objdump -xS $(OUTPUT) > $(OUTPUT).d
		arm-elf-objcopy -O ihex $(OUTPUT) $(OUTPUT).hex
		arm-elf-nm $(OUTPUT) > $(OUTPUT).n
	 
$(SOURCE)/startup.o: $(SOURCE)/startup.s
		$(CC) -c $(CFLAGS) $(SOURCE)/startup.s -o $(SOURCE)/startup.o

$(OBJ_FILES): %.o : %.c
		$(CC) -c $(CFLAGS) $< -o $@

#--------------------------------------------------------------------------------
#	loading
#--------------------------------------------------------------------------------
load: $(OUTPUT)
		$(LOADER) $(LOADERFLAGS) -flash $(OUTPUT).hex
		
#--------------------------------------------------------------------------------
#	rebuild
#--------------------------------------------------------------------------------
clean:
		rm -rf *.o *.elf *.gdb $(OUTPUT) *.r *.n *.d
		rm -rf $(SOURCE)/*.o

rebuild: clean $(OUTPUT)

#--------------------------------------------------------------------------------
#	ACRONYMS
#--------------------------------------------------------------------------------
t: $(OUTPUT)
l: load
c: clean
r: rebuild
rl: rebuild load